﻿var XmlHandle = function (url, rootnode) {
    this.protos = new Array();
    this.source = new Array();
    var temp = this;

    this.load = function (arrfun) {
        SendAjax("get", url, null, "xml", function (data) {
            var xmlobj = $(data).find(rootnode);

            //获取xml节点的tagName,并添加到数组
            xmlobj.eq(0).children().each(function () {
                temp.protos.push($(this).get(0).tagName);
            })
            //生成json数组
            xmlobj.each(function () {
                var tempjson = "";
                tempjson += "{";
                for (var i = 0; i < temp.protos.length; i++) {
                    tempjson += temp.protos[i] + ":'" + $(this).find(temp.protos[i]).eq(0).text() + "',";
                }
                tempjson = tempjson.substring(0, tempjson.lastIndexOf(','));
                tempjson += "}";
                var jsonobj = eval('(' + tempjson + ')');

                temp.source.push(jsonobj);
            })

            if (arrfun.length == 0 || arrfun == null) {
                return;
            }

            for (var i = 0; i < arrfun.length; i++) {
                arrfun[i].init(temp.protos, temp.source);
            }

            //清空
            temp.protos.length = 0;
            temp.source.length = 0;
        });
    }
}